package com.example.myapplication;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager2 viewPager;
    private VideoAdapter videoAdapter;
    private List<String> videoUrls = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = findViewById(R.id.viewPager);
        // 设置 ViewPager2 的方向为垂直
        viewPager.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
        // 初始化 Retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.110.148:8080") // 修改为你的 Go 后端地址
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        VideoService service = retrofit.create(VideoService.class);
        Call<List<Video>> call = service.getVideos();

        //showToast("Starting Retrofit call to get videos");

        call.enqueue(new Callback<List<Video>>() {
            @Override
            public void onResponse(Call<List<Video>> call, Response<List<Video>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    List<Video> videos = response.body();
                    if (!videos.isEmpty()) {
                        for (Video video : videos) {
                            videoUrls.add(video.getUrl());
                        }
                        List<Video> videoList = response.body();
                        videoAdapter = new VideoAdapter(videoList, MainActivity.this); // 使用 Video 列表初始化适配器
                        viewPager.setAdapter(videoAdapter);
                    } else {
                        //showToast("No videos found in the response");
                    }
                } else {
                    //showToast("Failed to fetch videos, Response code: " + response.code());
                }
            }

            @Override
            public void onFailure(Call<List<Video>> call, Throwable t) {
                //showToast("Error fetching videos: " + t.getMessage());
                t.printStackTrace();
            }
        });
    }

    private void showToast(String message) {
        Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
    }
}